/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import context.ContextEvolutif;
import context.IContext;

import java.io.PrintStream;
import java.util.Map;

/**
 *
 * eCOM - Adopte un Presta - Mars-Avril 2013
 * @author Kheira, Jef, William, Tuan-Long
 */
public final class HelpCommand extends CommandAbstract implements ICommandContextuelle 
{
	private IContext context;

	public HelpCommand() {
		this.context = ContextEvolutif.contextE;
	}

	public HelpCommand(IContext context) {
		this.context = context;
	}
	
	@Override
	public String getName() {
		return "help";
	}

	@Override
	public String getUsage() {
		return "help";
	}

	@Override
	public String getShortDescription() {
		return "Affiche l'aide pour ce client.";
	}
        
        @Override
	public IContext getContext() {
		return context;
	}

	@Override
	public void setContext(IContext context) {
		this.context = context;
	}

        // Affiche la liste des autres commandes, et leur usage
	@Override
	protected void execute(PrintStream out, PrintStream err) {

		Map<String, ICommand> commands = context.getCommands();
		int nbCommand = commands.size();

		String[] usage = new String[nbCommand];
		String[] desc = new String[nbCommand];

		int maxUsage = 0;
		int i = 0;
		for (ICommand command : commands.values()) {
			usage[i] = command.getUsage();
			desc[i] = command.getShortDescription();

			if ((usage[i] != null) && (desc[i] != null)) {
				maxUsage = Math.max(maxUsage, usage[i].length());
			}

			i++;
		}

		StringBuffer sb = new StringBuffer();
		i = 0;
		for (ICommand command : commands.values()) {
			if ((usage[i] != null) && (desc[i] != null))
			{
				sb.delete(0, sb.length());
				for (int j = 0; j < (maxUsage - usage[i].length()); j++)
				{
					sb.append(' ');
				}
				out.println(usage[i] + sb + " - " + desc[i]);
			}
			i++;
		}

	}



	
}
